package com.group4.admin.beans;

import com.group4.controller.CourseFacade;
import com.group4.controller.StudentFacade;
import com.group4.entity.Course;
import com.group4.entity.Student;
import com.group4.entity.User;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author andake, anjonas
 */
@ManagedBean
@RequestScoped
public class AdminStudentsBackingBean {
    
    @EJB
    private StudentFacade studentDb;
    @EJB
    private CourseFacade courseDb;
    
    private User user;
    private List<String> coursesId;

    /** Creates a new instance of AdminStudentsBackingBean */
    public AdminStudentsBackingBean() {
        user = new User();
    }
    
    public List<Student> getStudents() {
        List<Student> l = studentDb.findAll();
        return l;
    }
    
    public List<Course> getCourses() {
        return courseDb.findAll();
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<String> getCoursesId() {
        return coursesId;
    }

    public void setCoursesId(List<String> coursesId) {
        this.coursesId = coursesId;
    }
    
    // adds a new student
    public void addStudent() {
        Student newStudent = new Student();
        ArrayList<Course> courses = new ArrayList<Course>();
        for (String cid : coursesId) {
            courses.add(courseDb.find(Long.valueOf(cid)));
        }
        newStudent.setUser(user);
        newStudent.setsCourses(courses);
        studentDb.create(newStudent);
        user = new User();
    }
    
    // delete a student
    public void deleteStudent(Student s) {
        studentDb.remove(s);
    }
}
